/**
 * 
 */
package com.y7net.snakeplus;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.ViewGroup;

/**
 * @author Administrator
 *
 */
public class TileView extends ViewGroup {

	protected static int tileSize = 12;
	protected static float ss = 1.0f;
	protected static int xm = 0;
	protected static int ym = 0;
	
	protected static int xTileCount;
	protected static int yTileCount;
	
	protected static int xOffset;
	protected static int yOffset;
	
	//cell type
	public static final int TYPE_BLANK = 0;
	public static final int TYPE_SNAKE = 1;
	public static final int TYPE_SNAKE_HEAD = 11;
	public static final int TYPE_SNAKE_BODY = 12;
	public static final int TYPE_SNAKE_TAIL = 13;
	public static final int TYPE_TARGET = 2;
	public static final int TYPE_BARRIER = 3;
	public static final int TYPE_TREASURE = 4;
    
	private Cell tileGrid[][];
		
    protected int mDirection = NORTH;
    public static final int NORTH = 1;
    public static final int SOUTH = -1;
    public static final int EAST = 2;
    public static final int WEST = -2;
    
    public int dpi = 240;
    public int themeBgColor = 0xffffffff;
    private final Paint paint = new Paint();
	
	/**
	 * @param context
	 */
	public TileView(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
		setWillNotDraw(false);
	}

	/**
	 * @param context
	 * @param attrs
	 */
	public TileView(Context context, AttributeSet attrs) {
		super(context, attrs);
		// TODO Auto-generated constructor stub
		setWillNotDraw(false);
	}

	/**
	 * @param context
	 * @param attrs
	 * @param defStyle
	 */
	public TileView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		// TODO Auto-generated constructor stub
		setWillNotDraw(false);
	}
	
    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
    	
    	tileSize = (int)Math.min(w/40, h/44);
    	
    	if(dpi>0){
    		ss = (float)tileSize/(12*(float)(dpi/240f));
    	}
    	
    	xTileCount = (int)Math.floor(w/tileSize);
    	if(xTileCount>40){xTileCount = 40;}
    	yTileCount = (int)Math.floor(h/tileSize);
    	if(yTileCount>44){yTileCount = 44;}

    	xOffset = (w - xTileCount*tileSize)/2;
    	yOffset = (h - yTileCount*tileSize)/2;
    	
    	tileGrid = new Cell[xTileCount][yTileCount];
    	
    	sizeInited();
    }
    
    protected void sizeInited(){
    	initNewGame();
    }
    
    public void initNewGame(){
    	clearTile();
    }
    
    public void clearTile(){
    	for(int x=0; x < xTileCount; x++){
    		for(int y=0; y < yTileCount; y++){
    			setTile(x, y, new Cell(x, y, TileView.TYPE_BLANK));
    		}
    	}
    }
    
    public void setTile(int x, int y, Cell c){
    	if(tileGrid!=null && x>=0 && y>=0 &&  
    	   tileGrid.length>x && tileGrid[x].length>y){
    		tileGrid[x][y] = c;
    	}
    }
       
    public Bitmap getCellBitmap(Cell c){
    	return null;
    }
    
	public Cell getCellByXYpx(int x, int y){
		x = (int)x/tileSize;
		y = (int)y/tileSize;
		if(tileGrid!=null){
			return tileGrid[x][y];
		}
		return null;
	}
    
	@Override
	protected void onLayout(boolean changed, int l, int t, int r, int b) {
		// TODO Auto-generated method stub
	}
	
	@Override
    public void onDraw(Canvas canvas){
    	super.onDraw(canvas);
    	
    	
    	//draw offset background color
    	int w = this.getWidth();
    	int h = this.getHeight();
    	paint.setColor(this.themeBgColor);
    	canvas.drawRect(0, 0, xOffset, h, paint);
    	canvas.drawRect(0, 0, w, yOffset, paint);
    	canvas.drawRect(w-xOffset, 0, w, h, paint);
    	canvas.drawRect(0, h-yOffset, w, h, paint);
    	paint.setColor(Color.BLACK);
    	canvas.drawLine(xOffset, yOffset, w-xOffset, yOffset, paint);
    	canvas.drawLine(xOffset, yOffset, xOffset, h-yOffset, paint);
    	canvas.drawLine(xOffset, h-yOffset, w-xOffset, h-yOffset, paint);
    	canvas.drawLine(w-xOffset, yOffset, w-xOffset, h-yOffset, paint);
    	
    	//draw tile cells
    	for(int x=0; x < xTileCount; x++){
    		for(int y=0; y < yTileCount; y++){
    			Cell c = tileGrid[x][y];
    			if(c.type != TileView.TYPE_BLANK){
    				switch(c.type){
    				case TileView.TYPE_TARGET:
    					paint.setColor(Color.BLUE);
    					break;
    				case TileView.TYPE_TREASURE:
    					paint.setColor(Color.GREEN);
    					break;
    				case TileView.TYPE_BARRIER:
    					paint.setColor(Color.YELLOW);
    					break;
    				case TileView.TYPE_SNAKE:
    					paint.setColor(Color.WHITE);
    					break;
    				}
    				
    				Bitmap bitmap = getCellBitmap(c);
    				if(bitmap!=null){
        				canvas.drawBitmap(
        						getCellBitmap(c),
        						Float.parseFloat(String.valueOf(xOffset+tileSize*c.x)), 
        						Float.parseFloat(String.valueOf(yOffset+tileSize*c.y)), 
        						paint);
    				}else{
	    				canvas.drawRect(
								xOffset+tileSize*x, 
								yOffset+tileSize*y,
								xOffset+tileSize*(x+1),
								yOffset+tileSize*(y+1),
								paint);
    				}
    			}
    		}
    	}
    }
	
}
